#!/usr/bin/perl -w

unlink glob('*.data');

$source = shift or die("Must specify a source directory\n");

$source .= '/*.zip';
@source = glob($source);

foreach (@source) {{
  `unzip $_ ptscore.log`;
  open LOG, 'ptscore.log' or next;
  undef $turn;
  while(<LOG>) {
    if (/^Turn: (\d+)/) {
      $turn = $1;
      last;
    }
  }
  if (!defined($turn)) {
    unlink 'ptscore.log';
    next;
  }
  rename 'ptscore.log',sprintf('score%04d.log',$turn);
}}

while (<*.log>) {
  open FILE,$_;
  print("Opening $_\n\n");
  while(<FILE>) {
    if (/^Turn: (\d+)/) {
      $turn = $1;
      last if (defined $seen{$turn});
      $seen{$turn} = 1;
    }
    next if (!/^ +\d/);

    ($race, $name, $total, $used, $percent, $base, $planet, $ships, $warships,
     $planetcount, $basecount, $warshipcount, $shipcount) =
      /^ +(\d\d?) +(.+?) +(\d+) +(\d+) +(\d+) +(\d+) +(\d+) +(\d+) +(\d+) +(\d+) +(\d+) +(\d+) +(\d+) *$/;

    $racename[$race] = $name;
    open RACE, ">>race$race".'.data';
    print RACE "$turn $base $planet $ships $warships\n";
    close RACE;
    $score[$race]=$used;
  }
  open GLOB,">>global.data";
  print GLOB "$turn";
  foreach my $score (@score) {
    if (defined($score)) {
      print GLOB " $score";
      $score = 'X';
    }
  }
  print GLOB "\n";
  close GLOB;
}

$colour[1] = "red";
$colour[2] = "brightblue";
$colour[3] = "green";
$colour[4] = "orange";
$colour[5] = "gray(0.7)";
$colour[6] = "claret";  #darkred
$colour[7] = "darkblue";
$colour[8] = "brightgreen";
$colour[9] = "darkorange";
$colour[10] = "grey(0.4)";
$colour[11] = "lightpurple";

$races = 0;
foreach my $i (1..12) {
  if ($score[$i]) {
    $race[$races++] = $i;
  }
}

open PLOT,">global.ploticus";
print PLOT <<TOPBIT;
// Autogenerated Ploticus Global File
#set data = "global.data"

#set errcolor = black
#set errwidth = 0.08
#set errthick = 0.5
#set legend = "min+0.5 min-0.5"

#include \$chunk_setstd

#include \$chunk_read

#include \$chunk_area

xscaletype: categories
xcategories: datafield=1

TOPBIT

print PLOT "yautorange: datafields=";
foreach $i (1..(@race-1)) {
   print PLOT ($i+1 .",");
}
print PLOT (@race) + 1 . "\n";

print PLOT <<MIDBIT;
#include \$chunk_xaxis
stubs: usecategories
stubcull: 0.2

#include \$chunk_yaxis
stubcull: yes

#include \$chunk_title

#proc lineplot
xfield: 1
yfield: 2
legendsampletype: line
gapmissing: yes
MIDBIT

print PLOT 'linedetails: color=';
print PLOT $colour[$race[0]];
print PLOT "\n";
print PLOT 'legendlabel: ';
print PLOT $racename[$race[0]];
print PLOT "\n";

print PLOT "#saveas: L\n\n";

foreach $i (1..(@race - 1)) {
   print PLOT "#proc lineplot\n";
   print PLOT "#clone: L\n";
   print PLOT "yfield: ". ($i+2) ."\n";

   print PLOT 'linedetails: color=';
   print PLOT $colour[$race[$i]];
   print PLOT "\n";
   print PLOT 'legendlabel: ';
   print PLOT $racename[$race[$i]];
   print PLOT "\n\n";
}

print PLOT "#proc legend\nlocation: \@legend\n";

close PLOT;

#####

# ploticus -png -prefab lines cats=yes data=global.data x=1 y=2 y2=3 y3=4 y4=5 name=Feds name2=Gorns name3=Klingons name4=Borg

